#ifndef _PROTOCOL_H_
#define _PROTOCOL_H_

#include "../device/Device.h"
#include "../buffer/Buffer.h"
#include "../virtual/Virtual.h"


/*
	class : Protocol
	desc : This class is used to work with
		various communication protocols like jtag
		spi, uart, usart. It was made to be relatively 
		general so it would be usable regardless of whether
		you are using hardware or software to implement protocol
		This means it is a little easier to deal with.
*/
template<typename Implementation>
class Protocol : public Device,  public Buffer
{
	protected:
		//implementation of the protocol
		Implementation* implementation;
		Queue<unsigned char> in;
		Queue<unsigned char> out;

	public:
		//default constructor
		Protocol();
		//constructor sets the implementation
		Protocol(Implementation* i);
		//configures the protocol
		virtual void config();
		//tells if an error occured in transmission
		virtual bool error();
		//tells if parity is correct
		virtual bool parity();
		//runs the protocol doing a polling format.
		virtual void run();
		//initializes the protocol up to the user to use.
		virtual void init();
		//transmits data
		void transmitData();
		//receives new data
		void receiveData();
		//schedules a transmit interrupt
		void scheduleTransmitInterrupt();
		//schedules a receive interrupt
		void scheduleReceiveInterrupt();
		//returns if the transmit has finished
		bool transmitFinished() ;
		//returns if the receive has finished;
		bool receiveFinished();
		//resets the transmit flag
		void resetTransmit();
		//resets the receive flag
		void resetReceive();
};

#include "Protocol.cpp"

#endif
